/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  cn.hutool.crypto.SecureUtil
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.Button
 *  javafx.scene.control.PasswordField
 *  javafx.scene.layout.Pane
 */
package com.gys.controller.index;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.gys.common.business.GysUtil;
import com.gys.common.data.HttpJson;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import de.felixroske.jfxsupport.FXMLController;
import java.net.URL;
import java.util.HashMap;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.Pane;

@FXMLController
public class RestPasswordController
extends BaseController
implements Initializable {
    @FXML
    private PasswordField password;
    @FXML
    private PasswordField rePassword;
    @FXML
    private PasswordField oldPassword;
    @FXML
    private Pane restPasswordPane;
    @FXML
    private Button sureBtn;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
    }

    @FXML
    public void sureAction() {
        String oldPasswordStr = this.oldPassword.getText().trim();
        String passwordStr = this.password.getText().trim();
        String rePasswordStr = this.rePassword.getText().trim();
        Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", null);
        if (ObjectUtil.isEmpty(oldPasswordStr)) {
            alert.setContentText("\u8bf7\u8f93\u5165\u539f\u5bc6\u7801");
            alert.showAndWait();
            return;
        }
        if (ObjectUtil.isEmpty(passwordStr) || ObjectUtil.isEmpty(rePasswordStr)) {
            alert.setContentText("\u8bf7\u8f93\u5165\u65b0\u5bc6\u7801");
            alert.showAndWait();
            return;
        }
        if (!passwordStr.equals(rePasswordStr)) {
            alert.setContentText("\u4e24\u6b21\u5bc6\u7801\u4e0d\u4e00\u81f4");
            alert.showAndWait();
            return;
        }
        if (!RestPasswordController.checkPass(passwordStr)) {
            alert.setContentText("\u5bc6\u7801\u683c\u5f0f\u9519\u8bef(\u6570\u5b57+\u82f1\u6587\u5b57\u6bcd\u5927\u5c0f\u5199)");
            alert.showAndWait();
            return;
        }
        HashMap<String, String> param = new HashMap<String, String>();
        param.put("password", SecureUtil.md5((String)passwordStr));
        param.put("passwordOld", SecureUtil.md5((String)this.oldPassword.getText().trim()));
        HttpJson httpJson = GysUtil.restPassword(param);
        if (httpJson.isSuccess()) {
            alert.setContentText("\u5bc6\u7801\u4fee\u6539\u6210\u529f");
            alert.showAndWait();
            this.restPasswordPane.getScene().getWindow().hide();
        } else {
            alert.setContentText(httpJson.getMsg());
            alert.showAndWait();
        }
    }

    public static boolean checkPass(String pass) {
        if (pass.length() < 8) {
            return false;
        }
        int ls = 0;
        if (pass.matches(".*[a-z]+.*")) {
            ++ls;
        }
        if (pass.matches(".*\\d+.*")) {
            ++ls;
        }
        if (pass.matches(".*[A-Z]+.*")) {
            ++ls;
        }
        return ls >= 3;
    }
}

